package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


@Slf4j
@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        String password = employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);
        if (null==one){
            return R.error("登录失败,用户名错误");
        }
        if (!one.getPassword().equals(password)){
            return R.error("登录失败,密码错误");
        }
        if (one.getStatus()==0){
            return R.error("登录失败,员工已禁用");
        }
        request.getSession().setAttribute("employee",one.getId());
        one.setPassword(null);
        return R.success(one);
    };

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出登录");
    }

    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee, HttpServletRequest request){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
/*        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        long id = (long) request.getSession().getAttribute("employee");
        employee.setCreateUser(id);
        employee.setUpdateUser(id);*/

        boolean save = employeeService.save(employee);

        return save?R.success("添加成功"):R.error("添加失败");
    }


    @GetMapping("/page")
    public R<Page> selectByPage(int page,int pageSize,String name){
        //日志打印参数
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);

        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        //构造条件查询构造器
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper();

        //进行条件的非空判断
        qw.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        //根据更新时间进行排序
        qw.orderByDesc(Employee::getUpdateTime);

        //调用service进行数据查询
        employeeService.page(pageInfo,qw);

        //返回数据给前端页面
        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> updataStatus(@RequestBody Employee employee, HttpSession session){
/*        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser((Long)session.getAttribute("employee"));*/
        boolean b = employeeService.updateById(employee);
        return b? R.success("修改成功"):R.error("修改失败");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){

        Employee emp = employeeService.getById(id);

        if (emp!=null){
            return R.success(emp);
        }
        return R.error("没有查询到该id的信息");
    }
}
